package com.aiolos.wxdc.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * websocket服务端
 */
//@Component
//@ServerEndpoint("/websocket")
@Slf4j
public class WebSocket {

    private Session session;

    //存放session的容器
    private static CopyOnWriteArraySet<WebSocket> webSocketSet
            = new CopyOnWriteArraySet<>();

    @OnOpen
    public void onOpen(Session session){
        this.session = session;
        webSocketSet.add(this);
        log.info(" [websocket消息] 有新的连接,总数={}",webSocketSet.size());
    }

    @OnClose
    public void onClose(){
        webSocketSet.remove(this);
        log.info(" [websocket消息] 连接断开,总数={}",webSocketSet.size());
    }

    @OnMessage
    public void onMessage(String msg){
        log.info(" [websocket消息] 收到客户端发来的消息,msg={}",msg);
    }

    /**
     * 通过广播的方式发送消息到客户端
     * @param msg
     */
    public void sendMessage(String msg){
        for (WebSocket webSocket:webSocketSet){
            log.info(" [websocket消息] 广播消息到客户端,msg = {}",msg);
            try {
                webSocket.session.getBasicRemote().sendText(msg);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
